<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no" />
    <title>添加审批</title>
    <link rel="stylesheet" type="text/css" href="../../css/mui.min.css"/>
    <link rel="stylesheet" type="text/css" href="../../css/instruction.css"/>
    <link rel="stylesheet" type="text/css" href="../../css/commonality.css">
    <link rel="stylesheet" type="text/css" href="../../css/t_style.css"/>
    <script src="../../js/mui.min.js"></script>
    <script src="../../js/jquery.min.js"></script>
    <script src="../../js/vue.js"></script>
    <script src="../../js/main.js"></script>
    <script src="../../js/autosize.min.js"></script>
    <script src="../../js/jquery.flexText.min.js"></script>
</head>
<body>
<div  id="app">
    <header class="mui-bar mui-bar-nav">
    <span class="back" @click="back"></span>
    <span class="cross" @click="close"></span>
    <h1 class="mui-title my_mui-title">添加审批</h1>
</header>
    <div class="mui-content">
        <div class="module">
            <p>最后保存:{{ time|getData}}</p>
            <div style="clear:both;"></div>
        </div>
      <!--  <div class="module">
            <div class="mui-input-row" v-for="item in zgApplyTextExtends">
                <label>{{ item.title}}:</label>
                <input type="text" class="mui-input-clear" placeholder="请输入审批事由（必填）" v-model="cause" v-if="item.indexSort == 1">
                <div class="write-area" v-if="item.indexSort == 2" style="margin-left: 99px">
                    <textarea placeholder="请输入审批详情（必填）" v-model="detail"></textarea>
                </div>
            </div>
        </div>-->
        <div class="module">
            <div class="mui-input-row">
                <label>审批事由:</label>
                <div class="write-areas" >
                    <textarea v-model="cause" placeholder="请输入审批事由（必填）" ></textarea>
                </div>
            </div>
            <div class="mui-input-row">
                <label>审批详情:</label>
                <div class="write-areas" >
                    <textarea v-model="detail" placeholder="请输入审批详情（必填）"></textarea>
                </div>
            </div>
        </div>

        <div class="approver">
            <p>审批人<span>(点击头像可删除)</span></p>
            <div class="add-approver"  v-for="(item,index) in approvers" @click="del_c(index)">
                <div class="approver-img">{{item.name | getHName}}</div>
                <p class="add-person" v-text="item.name" >张华明</p>
            </div>
            <div class="add-approver">
                <div class="add-btn" @click="open_popup(2)"></div>
                <p class="add-person">添加</p>
            </div>
            <div style="clear:both;"></div>
        </div>
        <div class="approver">
            <p>抄送人<span>(审批通过后，通知抄送人)</span></p>
            <div class="add-approver"v-for="(item,index) in selected"  @click="del(index)">
                <div class="approver-img">{{item.name | getHName}}</div>
                <p class="add-person" v-text="item.name">刘斌</p>
            </div>
            <div class="add-approver">
                <div class="add-btn" @click="open_popup(1)"></div>
                <p class="add-person">添加</p>
            </div>
            <div style="clear:both;"></div>
        </div>
        <div class="footer">
             <div class="submit-btn">
                 <button class="mui-btn mui-btn-primary" @click="submit">提交</button>
             </div>
         </div>
    </div>
    <div class="popup-bg" v-show="dept">
        <header class="mui-bar mui-bar-nav">
            <span class="back" @click="go_back"></span>
            <h1 class="mui-title">选择接收人</h1>
        </header>
        <div class="popup-content">
            <div class="mui-input-row mui-search">
                <span class="mui-icon mui-icon-search" @click="search"></span>
                <input type="text" :class="{shortter:isSearch}" v-model="searchInfo" placeholder="搜索用户名">
                <span class="cancel"  @click="go_back" v-if="isSearch">取消</span>
                 <div class="crumbs" v-if="!isSearch">
                    <ul>
                        <li style="color: #2c9ae8" @click="show_info(true, 0)" v-text="companyName">天津中新智冠信息技术有限公司</li>
                        <li v-for="(item, index) in crumbs" @click="show_info(false, index)">&nbsp;>&nbsp;{{item.name}}</li>
                        <div class="clear"></div>
                    </ul>
                </div>
            </div>

            <ul class="popup-list" :class="isSearch?'popup-list-sec':'popup-list-fir'">
                <li v-for="(item, index) in depts">
                    <div class="fr popup-next" v-show="item.show" @click="show_next(index,1)" >
                        <span>{{item.total}}</span>
                        <img src="../../images/xuanzebumen_right.png" alt="">
                    </div>
                    <div class="popup-info">
                        <input type="checkbox" :checked="item.check" @click="toggle(index)">
                        <div class="report-top" style="padding: 6px 0;float: left;margin-left: 10px">
                            <div class="report-headimg fl" style="margin-top: 2px" v-if="item.show" v-text="item.hName"></div>
                            <div class="report-headimg fl" style="margin-top: 2px" v-else="item.show">{{item.name | getHName}}</div>
                            <div class="report-info">
                                <p style="line-height: 45px">{{item.name}}</p>
                            </div>
                        </div>
                    </div>
                    <div class="clear"></div>
                </li>
            </ul>
        </div>

        <div class="popup-footer">
            <div class="haveSelected">已选择: <span v-text="num==1?selected_num:checked_num"></span>人</div>
            <div class="popup-footer-btn fr" @click="confirm">
                确定
            </div>
            <div class="clear"></div>
        </div>
    </div>
    <div class="popup-bg" v-show="selectOnly">
        <header class="mui-bar mui-bar-nav">
            <span class="back" @click="go_back"></span>
            <h1 class="mui-title">选择接收人</h1>
        </header>
        <div class="popup-content">
            <div class="mui-input-row mui-search">
                <span class="mui-icon mui-icon-search" @click="search"></span>
                <input type="text" :class="{shortter:isSearch}" v-model="searchInfo" placeholder="搜索用户名">
                <span class="cancel"  @click="go_back" v-if="isSearch">取消</span>
                <div class="crumbs" v-if="!isSearch">
                    <ul>
                        <li style="color: #2c9ae8" @click="show_info(true, 0)" v-text="companyName">天津中新智冠信息技术有限公司</li>
                        <li v-for="(item, index) in crumbs" @click="show_info(false, index)">&nbsp;>&nbsp;{{item.name}}</li>
                        <div class="clear"></div>
                    </ul>
                </div>
            </div>
            <ul class="popup-list" :class="isSearch?'popup-list-sec':'popup-list-fir'">
                <li v-for="(item, index) in depts">
                    <div class="fr popup-next" v-show="item.show" @click="show_next(index,2)" >
                        <span>{{item.total}}</span>
                        <img src="../../images/xuanzebumen_right.png">
                    </div>
                    <div class="popup-info">
                        <input type="checkbox" :checked="item.check" @click="toggle_only(index)" v-show="!item.show">
                        <div class="report-top" style="padding: 6px 0;float: left;margin-left: 15px">
                            <div class="report-headimg fl" style="margin-top: 2px" v-if="item.show" v-text="item.hName"></div>
                            <div class="report-headimg fl" style="margin-top: 2px" v-else="item.show">{{item.name | getHName}}</div>
                            <div class="report-info">
                                <p style="line-height: 45px">{{item.name}}</p>
                            </div>
                        </div>
                    </div>
                    <div class="clear"></div>
                </li>
            </ul>
        </div>

        <div class="popup-footer">
            <div class="haveSelected">已选择: <span v-text="onlyOneArr.length"></span>人</div>
            <div class="popup-footer-btn fr" @click="confirm">
                确定
            </div>
            <div class="clear"></div>
        </div>
    </div>
</div>

</body>
<script>
        $(function () {
            $('textarea').flexText();
        });
         function UrlSearch(){
             var name,value;
             var str=location.href; //取得整个地址栏
             var num=str.indexOf("?");
             str=str.substr(num+1); //取得所有参数   stringvar.substr(start [, length ]

             var arr=str.split("&"); //各个参数放到数组里
             for(var i=0;i < arr.length;i++){
                 num=arr[i].indexOf("=");
                 if(num>0){
                     name=arr[i].substring(0,num);
                     value=arr[i].substr(num+1);
                     this[name]=value;
                 }
             }
         }
         var Request=new UrlSearch(); //实例化
        localStorage.companyId = Request.cid==null?6:Request.cid;
         localStorage.userName = Request.loginName;
        Request.cid=Request.cid==null?6:Request.cid;

        function sendInfoToJava(name) {
            //调用android程序中的方法，并传递参数
            javascript:WebView.showInfoFromJs(name);
        }
        var app = new Vue({
            el: "#app",
            data: function () {
                return{
                    creatUserName:localStorage.userName,
                    companyId:0,
                    popup: false,
                    users: [],
                    user: false,
                    onlyOneArr:[],
                    selectOnly:false,
                    temp:[],
                    userType:"",
                    checked_num: 0,
                    checked: [],
                    dept: false,
                    depts: [],
                    approvers:[],
                    selected_num: 0,
                    selected: [],
                    rid: [],
                    select_person: [],
                    num: 1,
                    not_finish: "",
                    cause: "",
                    detail: "",
                    zgApplyTextExtends:[
                        {
                            "title":"审批事由",	//标签
                            "content":"",		//标签内容
                            "indexSort":1
                        },	//标签顺序
                        {
                            "title":"审批详情",	//标签
                            "content":"",	//标签内容
                            "indexSort":2
                        }//标签顺序
                    ],
                    zgApplyApproveExtends:[],
                    zgApplyCcExtends:[],
                    groups: [],
                    group_name: [],
                    group: false,
                    pop_title:"",
                    time:"",
//                    **********搜索
                    searchInfo:"",
                    isSearch:false,
                    crumbs: [],
                    companyName: ""
                }
            },
            filters: {
               getData:function (input) {
                   var d = new Date(input);
                   var hour = d.getHours()<10?("0"+d.getHours()) : (d.getHours());
                   var minutes = d.getMinutes()<10?("0"+d.getMinutes()) : (d.getMinutes());
                   return  hour + ':' + minutes;
               },
               getHName: function(input) {
                  if(input != null){
                      return input.slice(-2);
                  }else{
                      return '';
                  }
              }
            },
            methods: {
                back: function () {
                    var name = "back";
                    sendInfoToJava(name);
                },
                close: function () {
                    var name = "close";
                    sendInfoToJava(name);
                },
                getNow: function() {
                    var d = new Date();
                    var year = d.getFullYear();
                    var month = d.getMonth() + 1;
                    var day = d.getDate() <10 ? '0' + d.getDate() : '' + d.getDate();
                    var hour = d.getHours();
                    var minutes = d.getMinutes();
                    var seconds = d.getSeconds();
                    return year+ '-' + month + '-' + day + ' ' + hour + ':' + minutes + ':' + seconds;
                },
                submit: function () {
                    var this_obj = this;
                    if(this_obj.cause == "" || this_obj.detail == ""){
                        mui.toast("您还未填写内容")
                    }else if(this_obj.approvers.length == 0 ){//|| this_obj.selected.length == 0
                        mui.toast("您还未添加汇报审批人!")
                    }else{
                        for(var i in this_obj.zgApplyTextExtends){
                            if(this_obj.zgApplyTextExtends[i].indexSort  == 1){
                                this_obj.zgApplyTextExtends[i].content = this_obj.cause;
                            }else{
                                this_obj.zgApplyTextExtends[i].content = this_obj.detail;
                            }
                        }
                        var url = "/apply/add";
                        var data = {
//                            "creatTime":"2017-11-08 16:00:00",	//创建时间
                            "creatTime":this.getNow(),	//创建时间
                            "creatUserName":Request.loginName,	//用户名
                            "companyId":Request.cid ,//公司id
                            "zgApplyApproveExtends":[],
                            "zgApplyCcExtends":[],
                            "zgApplyTextExtends":this_obj.zgApplyTextExtends
                        };
                        for(var i in this_obj.approvers){
                            var node ={
                                "approveUserName":this_obj.approvers[i].loginName,
                                "indexSort":parseFloat(i)+1
                            }
                            data.zgApplyApproveExtends.push(node);
                            console.log(data.zgApplyApproveExtends)
                        }
                        for(var i in this_obj.selected){
                            var node ={
                                "ccUserName":this_obj.selected[i].loginName
                            }
                            data.zgApplyCcExtends.push(node)
                        }
                        if(typeof(Request.cid) == "undefined" || typeof(Request.loginName) == "undefined"){
                            mui.toast("请重新登录");
                        }else {
                            common_ajax(url, data, function (res) {
                                mui.toast("添加审批成功");
                                window.location.href = 'approval_record.html?cid=' + Request.cid + '&' + 'loginName=' + Request.loginName;
                            }, function (res) {
                                mui.toast(res.errorMg);
                            });
                        }
                    }

                },
                search: function() {
                     var this_obj = this;
                     this_obj.isSearch = true;
                     var url = "/user/selectUserByCompanyId";
                     var postData = {
                         "companyId": Request.cid,//必須傳
                         "name": this_obj.searchInfo
                    }
                    if(typeof(Request.cid) == "undefined"){
                        mui.toast("请重新登录");
                    }else {
                        common_ajax(url, postData, function (res) {
                            this_obj.depts = res.obj;
                            if (this_obj.selectOnly) {
                                for (var i in this_obj.approvers) {
                                    for (var j in this_obj.depts) {
                                        if (this_obj.approvers[i]['loginName'] == this_obj.depts[j]['loginName']) {
                                            this_obj.depts[j]['check'] = true;
                                        } else {
                                            this_obj.depts[j]['check'] = false;
                                        }
                                    }
                                }
                            } else {
                                for (var i in this_obj.selected) {
                                    for (var j in this_obj.depts) {
                                        if (this_obj.selected[i]['loginName'] == this_obj.depts[j]['loginName']) {
                                            this_obj.depts[j]['check'] = true;
                                        } else if (!this_obj.depts[j]['check']) {
                                            this_obj.depts[j]['check'] = false;
                                        }
                                    }
                                }
                            }
                        }, function (res) {
                            mui.toast(res.errorMg);
                        });
                    }
                },
                save_time: function() {
                     var this_obj = this;
                     var date = new Date();
                     this_obj.time=date;
                     return date;
                   },
                open_popup: function (msg) {
                    var this_obj = this;
                    this_obj.dept = true;
                    var number = 0;
                    this_obj.rid.push(number);
                    if(msg == 2){
                        this_obj.selectOnly = true;
                    }else{
                        this_obj.selectOnly = false;
                    }
                    show_depts(number,msg);
                },
                show_next: function (index,msg) {
                    var this_obj = this;
                    if(this_obj.depts[index]['sel_able'] == true){
                        var number = this_obj.depts[index]['id'];
                        var name = this_obj.depts[index]['name'];
                        this_obj.crumbs.push({id: number,name: name});
                        this_obj.rid.push(number);
                        show_depts(number,msg);
                    }
                },
                toggle: function (index) {
                    var isHas = false;   //验证是否已存在  在抄送选人时应用
                    var this_obj = this;
                    var url = "/organization/dept";
                    if(this_obj.depts[index]['check']==null || this_obj.depts[index]['check'] == false){
                        this_obj.depts[index]['check'] = true;
                        this_obj.depts[index]['sel_able'] = false;
                        if(this_obj.depts[index]['show'] == true){
                            recursion(this_obj.depts[index]['id']);
                        }else{
                            if(this_obj.creatUserName == this_obj.depts[index]['loginName']){
                                mui.toast("自己不能为抄送人")
                                this_obj.depts[index]['check'] = false;
                            }else{
                                if(this_obj.approvers.length > 0){
                                    for(var i in this_obj.approvers){
                                        if(this_obj.approvers[i]['loginName'] != this_obj.depts[index]['loginName']){
                                            isHas = false;
                                           // this_obj.selected.push({id: this_obj.depts[index]['id'],hName: this_obj.depts[index]['hName'],name: this_obj.depts[index]['name'],loginName: this_obj.depts[index]['loginName']});
                                        }else{
                                            isHas = true;
                                            this_obj.depts[index]['check'] = false;
                                            mui.toast(this_obj.depts[index]['name'] +"已经为审批人!");
                                            break
                                        }
                                    }
                                   if(isHas == false){                                    //若isHas为false则说明approvers中不存在所选
                                        this_obj.selected.push({id: this_obj.depts[index]['id'],hName: this_obj.depts[index]['hName'],name: this_obj.depts[index]['name'],loginName: this_obj.depts[index]['loginName']});
                                    }
//                                   this_obj.selected=unique(this_obj.selected);         //抄送数组去重复
                                }else{
                                    this_obj.selected.push({id: this_obj.depts[index]['id'],hName: this_obj.depts[index]['hName'],name: this_obj.depts[index]['name'],loginName: this_obj.depts[index]['loginName']});
                                }
                            }
                            this_obj.selected_num = this_obj.selected.length;
                        }
                    }else{
                        this_obj.depts[index]['check'] = false;
                        this_obj.depts[index]['sel_able'] = true;
                        if(this_obj.depts[index]['show'] == true){
                            del(this_obj.depts[index]['id']);
                            function del(deptId) {
                                var data = {
                                    "companyId":Request.cid, //公司ID
                                    "deptId": deptId
                                };
                                if(typeof(Request.cid) == "undefined"){
                                    mui.toast("请重新登录");
                                }else {
                                    common_ajax(url, data, function (res) {
                                        if (res.code < 0) {
                                            mui.toast(res.errorMsg);
                                        } else {
                                            console.log(res);
                                            if (res.obj.depts.length == 0) {
                                                for (var j in res.obj.users) {
                                                    for (var y = 0; y < this_obj.selected.length; y++) {
                                                        if (this_obj.selected[y]['id'] == res.obj.users[j]['id']) {
                                                            this_obj.selected.splice(y, 1);
                                                            y--;
                                                        }
                                                    }
                                                }
                                            } else {
                                                for (var i in res.obj.depts) {
                                                    del(res.obj.depts[i]['id']);
                                                }
                                                for (var x in res.obj.users) {
                                                    for (var a = 0; a < this_obj.selected.length; a++) {
                                                        if (this_obj.selected[a]['id'] == res.obj.users[x]['id']) {
                                                            this_obj.selected.splice(a, 1);
                                                            a--;
                                                        }
                                                    }
                                                }
                                            }
                                            console.log(this_obj.selected);
                                            this_obj.selected_num = this_obj.selected.length;
                                        }
                                    }, function (res) {
                                        mui.toast("拉取联系人失败");
                                    });
                                }
                            }
                        }else{
                            for(var a = 0;a < this_obj.selected.length;a++){
                                if(this_obj.selected[a]['id'] == this_obj.depts[index]['id']){
                                    this_obj.selected.splice(a,1);
                                    a--;
                                }
                            }
                            this_obj.selected_num = this_obj.selected.length;
                        }
                    }
                },
                toggle_only: function(index) {
                    var this_obj = this;
                    var url = "/organization/dept";
                    if(this_obj.depts[index]['check']){
                        this_obj.depts[index]['check'] = false;
                        this_obj.onlyOneArr.splice(0,1)
                    }else{
                        for(var i in this_obj.depts){
                            this_obj.depts[i]['check'] = false;
                        }
                        this_obj.depts[index]['check'] = true;
                        this_obj.onlyOneArr.splice(0,1,{id: this_obj.depts[index]['id'],hName: this_obj.depts[index]['hName'],name: this_obj.depts[index]['name'],loginName: this_obj.depts[index]['loginName']});
                    }
                },
                confirm: function () {
                    var this_obj = this;
                    this_obj.searchInfo ="";
                    this_obj.dept = false;
                    this_obj.isSearch = false;
                    if(this_obj.selectOnly){
                        this_obj.selectOnly = false;
                        if(this_obj.selected.length > 0 ){
                            for(var i in this_obj.selected){
                                if(this_obj.onlyOneArr.length > 0 && this_obj.selected[i]['id'] == this_obj.onlyOneArr[0]['id']){
                                    mui.toast(this_obj.onlyOneArr[i]['name']+"已经为抄送人");
                                    this_obj.onlyOneArr = [];
                                }
                            }
                        }
                        if(this_obj.onlyOneArr.length > 0 && this_obj.onlyOneArr[0]['loginName'] == this_obj.creatUserName){
                            mui.toast("自己不能为审批人!");
                            this_obj.onlyOneArr = [];
                        }else{
                            for(var i in this_obj.onlyOneArr){
                                if(this_obj.approvers.length == 0){
                                    this_obj.approvers.push(this_obj.onlyOneArr[0]);
                                }else{
                                   /* for (var j in this_obj.approvers){
                                       if(this_obj.approvers[j]['loginName'] == this_obj.onlyOneArr[i]['loginName']){
                                           mui.toast("您已经选择了"+ this_obj.onlyOneArr[i]['name']);
                                       }else{*/
                                          this_obj.approvers.push(this_obj.onlyOneArr[i]);
                                        /*  this_obj.approvers=unique(this_obj.approvers);   //选取审批人数组去重复
                                       }
                                   }*/
                                }
                            }
                        }
                    }else{
                         var arr = unique(this_obj.selected);
                         this_obj.selected = arr.concat();
                     }
                },
                del: function (index) {
                    var this_obj = this;
                    this_obj.selected.splice(index, 1);
                },
                del_c:function(index) {
                  var this_obj = this;
                  this_obj.approvers.splice(index, 1);
                },
                go_back: function () {
                    var this_obj = this;
                    console.log(this_obj.rid);
                    if(this_obj.isSearch){
                        this_obj.isSearch = false;
                        this_obj.searchInfo = "";
                        if(this_obj.selectOnly){
                            show_depts(this_obj.rid[this_obj.rid.length-1],2);
                        }else{
                            show_depts(this_obj.rid[this_obj.rid.length-1],1);
                        }
                    }else{
                        if(this_obj.rid[this_obj.rid.length-1] == 0){
                            var btnArray = ['取消', '确定'];
                            mui.confirm('确定要取消本次操作吗？', '', btnArray, function(e) {
                                if (e.index == 1) {
                                    this_obj.dept = false;
                                    this_obj.selectOnly = false;
                                    this_obj.rid = [];
                                    this_obj.selected = [];
                                    this_obj.selected_num = this_obj.selected.length;
                                }
                            })
                        }else{
                            this_obj.crumbs.pop();
                            this_obj.rid.pop();
                            console.log(this_obj.rid);
                            var number = this_obj.rid[this_obj.rid.length-1];
                            if(this_obj.selectOnly){
                               show_depts(number,2);
                           }else{
                               show_depts(number,1);
                           }
                        }
                    }
                },
                show_info: function (flag, index) {
                    var this_obj = this;
                    if(flag == true){
                        if(this_obj.selectOnly){
                           show_depts(index,2);
                       }else{
                           show_depts(index,1);
                       }
                        this_obj.crumbs = [];
                    }else{
                        var number = this_obj.crumbs[index]['id'];
                       if(this_obj.selectOnly){
                          show_depts(number,2);
                      }else{
                          show_depts(number,1);
                      }
                        var len = this_obj.crumbs.length - (index + 1);
                        this_obj.crumbs.splice(index + 1, len);
                    }
                }
            }
        });
        app.save_time();
        setInterval(app.save_time(),20*1000);
        function unique(arr) {
            var result = [], isRepeated;
            for (var i = 0; i < arr.length; i++) {
                isRepeated = false;
                for (var j = 0; j < result.length; j++) {
                    if (arr[i]['loginName'] == result[j]['loginName']) {
                        isRepeated = true;
//                        mui.toast("您已经选择了"+arr[i].name);
                        break;
                    }
                }
                if (!isRepeated) {
                    result.push(arr[i]);
                }
            }
            return result;
        }
        //已经选好的的添加到当前数组中
        function arrayIntersection(a, b){
            for(var i=0;i<b.length;i++){
                for(var j=0;j<a.length;j++){
                    if(b[i].id==a[j].id){
                        b[i].check=true;
                        break;
                    }
                }
            }
        }
        //下一级全选则本级显示选中
        function check_group(department,select){
            var all_select=0;
            for(var i=0;i<department.length;i++){
                var now_select=0;
                for(var j=0;j<select.length;j++){
                    if(department[i].id==select[j].id){
                        now_select=1;
                        break;
                    }
                }
                if(now_select!=1)break;
                if(i==department.length-1){
                    all_select=1;
                }
            }
            return all_select;
        }
        function show_depts(deptId,msg) {
            var url = "/organization/dept";
            var data = {
                "companyId": Request.cid, //公司ID
                "deptId": deptId // 初始传入0
            };
            if(typeof(Request.cid) == "undefined"){
                mui.toast("请重新登录");
            }else {
                common_ajax(url, data, function (res) {
                    if (res.code < 0) {
                        mui.toast(res.errorMsg);
                    } else {

                        app.depts = [];
                        for (var i in res.obj.depts) {
                            var hName = res.obj.depts[i]['name'].substr(0, 2);
                            app.depts.push({
                                id: res.obj.depts[i]['id'],
                                total: res.obj.depts[i]['total'],
                                hName: hName,
                                name: res.obj.depts[i]['name'],
                                check: false,
                                show: true,
                                sel_able: true
                            });
                            check_recursion(app.depts[i]['id'], i);
                        }
                        for (var j in res.obj.users) {
                            var eName = res.obj.users[j]['name'].slice(-2);
                            app.depts.push({
                                id: res.obj.users[j]['id'],
                                total: res.obj.users[j]['total'],
                                hName: eName,
                                name: res.obj.users[j]['name'],
                                check: false,
                                show: false,
                                sel_able: true,
                                loginName: res.obj.users[j]['loginName']
                            });
                        }
                        if (msg == 1) {
                            arrayIntersection(app.selected, app.depts);
                        } else if (msg == 2) {
                            arrayIntersection(app.onlyOneArr, app.depts);
                        }
                    }
                }, function (res) {
                    mui.toast("拉取联系人失败");
                })
            }
        }
        function check_recursion(deptId, index) {
            var url = "/organization/dept";
            var data = {
                "companyId": Request.cid, //公司ID
                "deptId": deptId
            };
            if(typeof(Request.cid) == "undefined"){
                mui.toast("请重新登录");
            }else {
                common_ajax(url, data, function (res) {
                    if (res.code < 0) {
                        mui.toast(res.errorMsg);
                    } else {
                        app.select_person = [];
                        if (res.obj.depts.length == 0) {
                            for (var j in res.obj.users) {
                                var eName = res.obj.users[j]['name'].slice(-2);
                                app.select_person.push({
                                    id: res.obj.users[j]['id'],
                                    hName: eName,
                                    name: res.obj.users[j]['name'],
                                    loginName: res.obj.users[j]['loginName']
                                });
                            }
                        } else {
                            for (var i in res.obj.depts) {
                                check_recursion(res.obj.depts[i]['id']);
                            }
                            for (var x in res.obj.users) {
                                var myName = res.obj.users[x]['name'].slice(-2);
                                app.select_person.push({
                                    id: res.obj.users[x]['id'],
                                    hName: myName,
                                    name: res.obj.users[x]['name'],
                                    loginName: res.obj.users[x]['loginName']
                                });
                            }
                        }
                        if (check_group(app.select_person, app.selected) == 1) {
                            app.depts[index]['check'] = true;
                        }
                    }
                }, function (res) {
                    mui.toast("拉取联系人失败");
                });
            }
        }
        function recursion(deptId) {
            var isHas = false;   //验证是否已存在  在抄送选人时应用
            var url = "/organization/dept";
            var data = {
                "companyId": Request.cid, //公司ID
                "deptId": deptId
            };
            if(typeof(Request.cid) == "undefined"){
                mui.toast("请重新登录");
            }else {
                common_ajax(url, data, function (res) {
                    if (res.code < 0) {
                        mui.toast(res.errorMsg);
                    } else {
                        console.log(res);
                        if (res.obj.depts.length == 0) {
                            for (var j in res.obj.users) {
                                var eName = res.obj.users[j]['name'].slice(-2);
                                if (app.creatUserName == res.obj.users[j]['loginName']) {
                                    mui.toast("自己不能为抄送人")
                                    for (var i in app.depts) {
                                        if (app.depts[i]['id'] == deptId) {
                                            app.depts[i]['check'] = false
                                        }
                                    }
                                } else {
                                    if (app.approvers.length > 0) {
                                        for (var i in app.approvers) {
                                            if (app.approvers[i]['loginName'] != res.obj.users[j]['loginName']) {
                                                //  app.selected.push({id: res.obj.users[j]['id'],hName: eName,name: res.obj.users[j]['name'],loginName: res.obj.users[j]['loginName']});
                                                isHas = false;
                                            } else {
                                                isHas = true;
                                                mui.toast(res.obj.users[j]['name'] + "已经为审批人!");
                                                for (var i in app.depts) {
                                                    if (app.depts[i]['id'] == deptId) {
                                                        app.depts[i]['check'] = false
                                                    }
                                                    break;
                                                }
                                            }
                                        }
                                        if (isHas == false) {
                                            app.selected.push({
                                                id: res.obj.users[j]['id'],
                                                hName: eName,
                                                name: res.obj.users[j]['name'],
                                                loginName: res.obj.users[j]['loginName']
                                            });
                                        }
                                    } else {
                                        app.selected.push({
                                            id: res.obj.users[j]['id'],
                                            hName: eName,
                                            name: res.obj.users[j]['name'],
                                            loginName: res.obj.users[j]['loginName']
                                        });
                                    }

                                }
//                            app.selected.push({id: res.obj.users[j]['id'],hName: eName,name: res.obj.users[j]['name'],loginName: res.obj.users[j]['loginName']});
                            }
                        } else {
                            for (var i in res.obj.depts) {
                                recursion(res.obj.depts[i]['id']);
                            }
                            for (var x in res.obj.users) {
                                var myName = res.obj.users[x]['name'].slice(-2);
//                            app.selected.push({id: res.obj.users[x]['id'],hName: myName,name: res.obj.users[x]['name'],loginName: res.obj.users[x]['loginName']});
                                if (app.creatUserName == res.obj.users[x]['loginName']) {
                                    mui.toast("自己不能为抄送人")
                                    for (var i in app.depts) {
                                        if (app.depts[i]['id'] == deptId) {
                                            app.depts[i]['check'] = false
                                        }
                                    }
                                } else {
                                    if (app.approvers.length > 0) {
                                        for (var i in app.approvers) {
                                            if (app.approvers[i]['loginName'] != res.obj.users[x]['loginName']) {
                                                //app.selected.push({id: res.obj.users[x]['id'],hName: myName,name: res.obj.users[x]['name'],loginName: res.obj.users[x]['loginName']});
                                                isHas = false;
                                            } else {
                                                mui.toast(res.obj.users[x]['name'] + "已经为审批人!");
                                                for (var i in app.depts) {
                                                    if (app.depts[i]['id'] == deptId) {
                                                        app.depts[i]['check'] = false
                                                    }
                                                    break;
                                                }
                                            }
                                        }
                                        if (isHas == false) {
                                            app.selected.push({
                                                id: res.obj.users[x]['id'],
                                                hName: myName,
                                                name: res.obj.users[x]['name'],
                                                loginName: res.obj.users[x]['loginName']
                                            });
                                        }

                                    } else {
                                        app.selected.push({
                                            id: res.obj.users[x]['id'],
                                            hName: myName,
                                            name: res.obj.users[x]['name'],
                                            loginName: res.obj.users[x]['loginName']
                                        });
                                    }
                                }
                            }
                        }
                        console.log(app.selected)
                        app.selected_num = app.selected.length;
                    }
                }, function (res) {
                    mui.toast("拉取联系人失败");
                });
            }
        }
        function sendInfoToJava(name) {
            //调用android程序中的方法，并传递参数
            javascript:WebView.showInfoFromJs(name);
        }
        if(typeof(Request.cid) == "undefined"){
            mui.toast("请重新登录");
        }else {
            var name = "companyId" + Request.cid;
            sendInfoToJava(name);
        }

        //在android代码中调用此方法
        function showInfoFromJava(msg){
            app.companyName = msg;
        }


</script>
</html>